package game.editer.ui;

import hx.events.MouseEvent;
import hx.layout.AnchorLayout;
import hx.layout.AnchorLayoutData;
import hx.display.DisplayObjectRecycler;
import hx.display.Label;
import hx.display.Quad;
import hx.display.ItemRenderer;
import hx.display.ListView;

/**
 * 文本列表显示组件
 */
class LabelListView extends ListView {
	override function onInit() {
		super.onInit();
		this.itemRendererRecycler = DisplayObjectRecycler.withClass(LabelListViewItem);
	}
}

typedef LabelListViewData = {
	var text:String;
	var data:Dynamic;
}

/**
 * 文本列表项渲染器
 */
class LabelListViewItem extends ItemRenderer {
	/**
	 * 文本
	 */
	public var label:Label;

	/**
	 * 背景
	 */
	public var bg:Quad;

	/**
	 * 选中背景
	 */
	public var selectedBg:Quad;

	override function onInit() {
		super.onInit();
		bg = new Quad(100, 30, 0x131314);
		bg.layoutData = AnchorLayoutData.fill(0);
		this.addChild(bg);

		selectedBg = new Quad(100, 30, 0x3D3D41);
		selectedBg.layoutData = AnchorLayoutData.fill(0);
		this.addChild(selectedBg);
		selectedBg.visible = false;

		label = new Label("选项1", Config.COMMON_TEXT_FORMAT);
		this.addChild(label);
		label.horizontalAlign = CENTER;
		label.verticalAlign = MIDDLE;
		this.layout = new AnchorLayout();
		label.layoutData = AnchorLayoutData.fill(0);
		this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
		this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
	}

	private function onMouseOver(event:MouseEvent):Void {
		bg.data = 0x303030;
	}

	private function onMouseOut(event:MouseEvent):Void {
		bg.data = 0x131314;
	}

	override function set_data(value:Dynamic):Dynamic {
		if (value != null) {
			var info:LabelListViewData = value;
			label.data = info.text;
		}
		return super.set_data(value);
	}

	override function set_selected(value:Bool):Bool {
		selectedBg.visible = value;
		return super.set_selected(value);
	}
}
